//
//  FxLocation.swift
//  Uber
//
//  Created by chen on 16/3/11.
//  Copyright © 2016年 chen. All rights reserved.
//

import UIKit
import CoreLocation

class FxLocation: NSObject,CLLocationManagerDelegate {
    var manager:CLLocationManager?
    func startLocation(){
        if CLLocationManager.locationServicesEnabled(){
            manager = CLLocationManager()
            manager?.delegate = self
            //控制定位精度，越高耗电量越大
            manager?.desiredAccuracy = kCLLocationAccuracyBest
            //控制定位服务更新频率，单位是米
            manager?.distanceFilter = 100
            //更新我们的位置信息
            manager?.startUpdatingLocation()
            //ios8接收用户授权
            manager?.requestAlwaysAuthorization()
        }
    }
    func searchAddress(location:CLLocation){
        let coder = CLGeocoder()
        coder.reverseGeocodeLocation(location) { (marks, error) -> Void in
            if marks != nil{
                for mark in marks!{
                    FxLog(mark.name!)
                }
            }
        }
    }
    //打印出我们当前的位置状态信息
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        FxLog(String(status))
    }
    //更新位置信息
    func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
        FxLog(newLocation.description)
        searchAddress(newLocation)
    }
    //
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        FxLog(locations[0].description)
        searchAddress(locations[0])
        //停止更新位置信息
        manager.stopUpdatingLocation()
    }

}
